using UnityEngine;
using System.Collections;
using Pathfinding;

public class EnemyControler : MonoBehaviour {
	
	public ArrayList enemyArray = new ArrayList();
	private ArrayList enemyClassArray = new ArrayList();
	private int newEnemyTimer = 0;
	private GameObject enemyStartPosition;
	private GameObject enemyEndPosition;
	private Seeker seeker;
	private GameObject pathCalculator;
	private GameObject newEnemy;// = new GameObject();
	//private AstarAI astarai;
	private Path currentPath;
	private TurretManager theTurretManager;
	
	// ENEMY TYPES HERE
	public GameObject enemy2;
	
	// Use this for initialization
	void Start () {
		newEnemy = new GameObject();
		enemyStartPosition = GameObject.Find("EnemyStartPosition");
		enemyEndPosition = GameObject.Find("EnemyEndPosition");
		pathCalculator = GameObject.Find("PathCalculator");
		seeker = pathCalculator.GetComponent<Seeker>();
		ArrayList bob = new ArrayList();
		GameObject temp = GameObject.Find("ControlContainer");
		theTurretManager = temp.GetComponent<TurretManager>();
		
	}
	
	// USED TO ADD ENEMIES TO THE ENEMY ARRAY
	public void addEnemy(GameObject unit){
		enemyArray.Add(unit);
		enemyClassArray.Add(unit.GetComponent<Enemy>());
		theTurretManager.enemyList = enemyArray;
	}
	
	// USED TO REMOVE ENEMIES FROM THE ENEMY ARRAY
	public void removeEnemy(GameObject unit){
		for(int i = 0; i < enemyArray.Count; i++){
			if(enemyArray[i] == unit){
				GameObject.Destroy(unit);
				enemyArray.RemoveAt(i);
				enemyClassArray.RemoveAt(i);
				theTurretManager.enemyList = enemyArray;
				return;
			}
		}
	}
	
	// USED TO GET A NEW PATH FROM THE SEEKER OBJECT
	public void getNewPath(){
		seeker.StartPath (enemyStartPosition.transform.position,enemyEndPosition.transform.position, spreadNewPath);
	}
	
	// USED TO SPREAD THE NEW PATH TO ALL ENEMIES, SHOULD BE CALLED AUTOMATICALLY BY getNewPath()
	public void spreadNewPath(Path p){
		Debug.Log ("Yey, we got a path back. Did it have an error? "+p.error);
        if (!p.error) {
            currentPath = p;
			for(int i = 0; i < enemyClassArray.Count; i++){
				Enemy editEnemy = (Enemy) enemyClassArray[i];
				editEnemy.takePath(p.vectorPath);
			}
        }
	}
	
	// USED TO CREATE NEW ENEMIES OF CERTAIN TYPES
	public GameObject createNewEnemy(string enemyType){
		switch (enemyType){
			case "Tank":
				// CODE HERE
				if(enemy2 != null){
					newEnemy = (GameObject) Instantiate(enemy2, enemyStartPosition.transform.position, enemyStartPosition.transform.rotation);
				}
				addEnemy(newEnemy);
				break;
			case "Person":
				// CODE HERE
				break;
		default:
				// CODE HERE
			Debug.Log("No enemy type match");
			break;
		}
		
		if(currentPath != null){
			newEnemy.GetComponent<Enemy>().takePath(currentPath.vectorPath);
		}//}
		return newEnemy;
		
	}
	
	// USED TO SPAWN NEW ENEMIES AT 100MS INTERVALS
	// Update is called once per frame
	void Update () {
		if(newEnemyTimer == 100 ){
			createNewEnemy("Tank");
			newEnemyTimer = 0;
			if(enemyArray.Count == 1){
				getNewPath();
			}
		} else {
			newEnemyTimer++;
		}
	}
}
